﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using Entities;

namespace Data
{
   public class SachRepository
   {
       
       public static List<SachDTO> GetSachList()
       {
           
           NhaSachEntities context = new NhaSachEntities();
           
           var q = from s in context.SachList
                   select new SachDTO { 
                       MaSach=s.MaSach,
                       TenSach=s.TenSach,
                       TheLoai=(int)s.TheLoai.MaTheLoai,
                       MaTacGia=s.TacGia.MaTacGia,
                       SoLuong=(int)s.SoLuong,
                       DonGia=(double)s.DonGia,
                       TinhTrang=(int)s.TinhTrang

                   };
           return q.ToList();
       }
       public static SachDTO GetSachByMaSach(int maSach)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from s in context.SachList
                    where s.MaSach == maSach
                    select new SachDTO()
                    {
                        MaSach =(int) s.MaSach,
                        TenSach = s.TenSach,
                        TheLoai = (int)s.TheLoai.MaTheLoai,
                        MaTacGia = (int)s.TacGia.MaTacGia,
                        SoLuong = (int)s.SoLuong,
                        DonGia = (double)s.DonGia,
                        TinhTrang = (int)s.TinhTrang
                    }).FirstOrDefault();


           return q;
       }
       public static List<SachDTO> SearchByTenSach(String tenSach)
       {
           
           NhaSachEntities context=new NhaSachEntities();
           var q = from s in context.SachList
                    where s.TenSach.Contains(tenSach) && s.TinhTrang==1
                    select new SachDTO()
                    {
                        MaSach = (int)s.MaSach,
                        TenSach = s.TenSach,
                        TheLoai = (int)s.TheLoai.MaTheLoai,
                        MaTacGia = (int)s.TacGia.MaTacGia,
                        SoLuong = (int)s.SoLuong,
                        DonGia = (double)s.DonGia,
                        TinhTrang = (int)s.TinhTrang
                    };
           return q.ToList();
       }
       public static List<SachDTO> Search(string tenSach, string tenTacGia, string tenTheLoai
           ,int soLuong, int khoangSoLuong, double gia, int khoangGia)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = 
    from sach in context.SachList
    join tl in context.TheLoaiList on sach.MaSach equals tl.MaTheLoai into temp
    from s in temp 
    join tg in context.TacGiaList on  equals tg.MaTacGia
    where s.TinhTrang==1 
                   select new SachDTO() {
                       MaSach = (int)s.MaSach,
                       TenSach = s.TenSach,
                       TheLoai = (int)s.TheLoai.MaTheLoai,
                       MaTacGia = (int)s.TacGia.MaTacGia,
                       SoLuong = (int)s.SoLuong,
                       DonGia = (double)s.DonGia,
                       TinhTrang = (int)s.TinhTrang
                   };
                 return q.ToList();
                 
       }
       
    }
}
